package com.qs.service;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.jsp.JspWriter;

import com.infomanage.authority.AuthorityBean;
import com.infomanage.bean.FieldBean;
import com.infomanage.bean.PaginationBean;
import com.infomanage.bean.SearchBean;
import com.infomanage.dao.BaseDAO;
import com.infomanage.enums.RoleType;
import com.infomanage.po.User;
import com.infomanage.util.Consts;
import com.infomanage.util.StringUtil;
import com.jiuqi.util.ReturnObject;
import com.jiuqi.util.StringUtils;
import com.qs.po.QsEquClass;

public class EquipmentClassService {

	public static QsEquClass load(HttpServletRequest request, HttpServletResponse response,
			JspWriter out) {
		try {
			request.setCharacterEncoding("UTF-8");
			String itemId = request.getParameter("itemId");
			BaseDAO<QsEquClass> dao = new BaseDAO<QsEquClass>();
			if (StringUtils.isEmpty(itemId))
				return null;
			return (QsEquClass) dao.find(QsEquClass.class, Integer.parseInt(itemId));
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	
	public static List<QsEquClass> loadList(HttpServletRequest request, HttpServletResponse response,
			JspWriter out) {
		try {
			request.setCharacterEncoding("UTF-8");
			BaseDAO<QsEquClass> dao = new BaseDAO<QsEquClass>();
			List<QsEquClass> list = dao.findAll(QsEquClass.class);
			return list;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	
	public static ReturnObject save(HttpServletRequest request, HttpServletResponse response, JspWriter out) {
		ReturnObject ret = new ReturnObject();
		try {
			request.setCharacterEncoding("UTF-8");
			QsEquClass bean = new QsEquClass();
			bean.setDescription(request.getParameter("description"));
			bean.setTitle(request.getParameter("title"));
			BaseDAO<QsEquClass> dao = new BaseDAO<QsEquClass>();
			dao.add(bean);
		} catch (Exception e) {
			ret.SetErrored(true);
			ret.setErrorMessage(e.getMessage());
		}
		return ret;
	}

	public static ReturnObject remove(HttpServletRequest request, HttpServletResponse response, JspWriter out) {
		ReturnObject ret = new ReturnObject();
		try {
			request.setCharacterEncoding("UTF-8");
			String itemId = request.getParameter("itemId");
			BaseDAO<QsEquClass> dao = new BaseDAO<QsEquClass>();
			dao.remove(QsEquClass.class, Integer.parseInt(itemId));
		} catch (Exception e) {
			ret.SetErrored(true);
			ret.setErrorMessage(e.getMessage());
		}
		return ret;
	}
	
	public static ReturnObject modify(HttpServletRequest request, HttpServletResponse response, JspWriter out) {
		ReturnObject ret = new ReturnObject();
		try {
			request.setCharacterEncoding("UTF-8");
			int itemId = Integer.parseInt(request.getParameter("itemId"));
			BaseDAO<QsEquClass> dao = new BaseDAO<QsEquClass>();
			QsEquClass bean = dao.find(QsEquClass.class, itemId);
			bean.setDescription(request.getParameter("description"));
			bean.setTitle(request.getParameter("title"));
			dao.modify(bean);
		} catch (Exception e) {
			ret.SetErrored(true);
			ret.setErrorMessage(StringUtil.filterIllegalchar(e.getMessage()));
		}
		return ret;
	}
	
	public static List<QsEquClass> search(HttpServletRequest request, HttpServletResponse response, JspWriter out) {
		try {
			request.setCharacterEncoding("UTF-8");
			BaseDAO<QsEquClass> dao = new BaseDAO<QsEquClass>();
			SearchBean searchBean = buildSearchCondition(request, response, out);
			// 设置分页
			String currPageNum = request.getParameter("currPageNum");
			PaginationBean page = new PaginationBean();
			if (currPageNum == null) {
				page.setCurrentPageNum(1);
			} else {
				page.setCurrentPageNum(Integer.parseInt(currPageNum));

			}
			searchBean.setPage(page);
			List<QsEquClass> list = dao.findByFields(QsEquClass.class, searchBean);
			return list;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	
	public static int getPageCount(HttpServletRequest request, HttpServletResponse response, JspWriter out) {
		try {
			BaseDAO<QsEquClass> dao = new BaseDAO<QsEquClass>();
			SearchBean searchBean = buildSearchCondition(request, response, out);
			return (dao.count(QsEquClass.class, searchBean) + PaginationBean.pageSize - 1) / PaginationBean.pageSize;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return 1;
	}
	
	private static SearchBean buildSearchCondition(HttpServletRequest request, HttpServletResponse response,
			JspWriter out) throws Exception {
		request.setCharacterEncoding("UTF-8");
		String title = request.getParameter("title");
		SearchBean searchBean = new SearchBean();
		if (!StringUtil.isEmpty(title)) {
			FieldBean field = new FieldBean("title", title);
			field.setLike(true);
			searchBean.add(field);
		}

		// 设置权限
		User user = (User) request.getSession().getAttribute(Consts.LOGINED_USER);
		if (user != null) {
			AuthorityBean authority = new AuthorityBean();
			authority.setRole(RoleType.valueOf(user.getRoleId()));
			authority.setDeptId(user.getAssociatedDept());
			searchBean.setAuthority(authority);
		}
		return searchBean;
	}
}
